<?php

namespace App\Repositories;

use App\User;
use App\ArticleSub;

class ArticleSubRepository
{
    /**
     * Get all of the tasks for a given user.
     *
     * @param  User  $user
     * @return Collection
     */
    public function forUser(User $user)
    {
        return ArticleSub::where('user_id', $user->id)
                ->orderBy('created_at', 'asc')
                ->get();
    }
    
    /**
     * Get all of the tasks for a given user.
     *
     * @param  User  $user
     * @return Collection
     */
    public function forUserByStatus(User $user,string $status,$need_page=false,$page_size=30)
    {
    	$article = ArticleSub::with('article.feed')->where('user_id', $user->id)
		    	->where('status',$status)->orderBy('updated_at','desc');
    	
    	if($need_page){
    		return $article->paginate($page_size);
    	} else {
    		return $article->get();
    	}
    }
	/**
     * Get all of the tasks for a given user.
     *
     * @param  User  $user
     * @return Collection
     */
    public function forUserByCategoryStatusFeedId(User $user,string $status,$category_id,$need_page=false,$page_size=30)
    {
    	$feedsubs = \DB::table('feed_subs')->select('feed_id')->where('category_id',$category_id)->where('status',1)->get();
    	
    	$feed_id_arr = array();
    	foreach ($feedsubs as $feedsub){
    		$feed_id_arr[] = $feedsub->feed_id;
    	}
    	
    	$article = ArticleSub::with('article.feed')->where('user_id', $user->id)
    	->whereIn('feed_id', $feed_id_arr)
    	->where('status',$status);
    	
//     	$article = ArticleSub::with('article.feed')->where('user_id', $user->id)
// 		->whereIn('feed_id', function($query) use($category_id){
//             \Log::info('sub query start:'.time());
// 			$query->select('feed_id')
// 			->from('feed_subs')
// 			->where('category_id', $category_id)
// 			->where('status', 1);
//             \Log::info('sub query end:'.time());
// 		})
//     	->where('status',$status);
        /*
        $article = \DB::table('article_subs')->with('articles')
       ->where(['article_subs.user_id'=>$user->id])
       ->where(['article_subs.status'=>$status])
       ->join('articles', 'articles.id', '=', 'article_subs.article_id')
       ->leftJoin("feed_subs",'feed_subs.feed_id','=','article_subs.feed_id')
	   ->where(['feed_subs.category_id'=>$category_id])
	   ->where(['feed_subs.status'=>1]);*/
       
    	if($need_page){
    		return $article->paginate($page_size);
    	} else {
    		return $article->get();
    	}
    }
    
    public function forUserByStatusFeedId(User $user,string $status,$feed_id,$need_page=false,$page_size=30)
    {
    	$article = ArticleSub::with('article.feed')->where('user_id', $user->id)
    	->where('status',$status)
    	->where('feed_id',$feed_id)->orderBy('updated_at','desc');
    	if($need_page){
    		return $article->paginate($page_size);
    	} else {
    		return $article->get();
    	}
    }
    
    public function getRecentPublishList(User $user,string $status,$start_time,$end_time,$limit){
    	return ArticleSub::with('article.feed')->where('user_id',$user->id)->where('status',$status)->where('published','<',$end_time)->where('published','>',$start_time)->orderBy('feed_id')->limit($limit)->get();
    }
    
}
